package routers

import (
	"github.com/emicklei/go-restful"
	"kubeInstall/pkg/logger"
	"kubeInstall/server/handlers"
	"kubeInstall/server/handlers/cluster"
	"kubeInstall/server/handlers/credential"
	"kubeInstall/server/handlers/file"
	"kubeInstall/server/handlers/host"
	"kubeInstall/server/handlers/registry"
	"kubeInstall/server/handlers/storageplugin"
	"kubeInstall/server/handlers/user"
)

type Router struct {
	Ws []*restful.WebService
}

func InitRouter() *Router {
	r := new(Router)
	r.Ws = append(r.Ws, r.initDefaultWebService(), r.initLogin(), r.initWsconn())
	logger.Info("add all webService successfully")
	return r
}

func (r *Router) initDefaultWebService() *restful.WebService {
	ws := new(restful.WebService)
	ws.Path("/api/v1").Filter(handlers.NewMiddlewareHandler().ValidateTokenFilter)

	user.NewUserHandler().AddToRoute(ws)
	host.NewHostHandler().AddToRoute(ws)
	credential.NewCredentialHandler().AddToRoute(ws)
	storageplugin.NewNfsHandler().AddToRoute(ws)
	cluster.NewClusterHandler().AddToRoute(ws)
	registry.NewRepositoryHandler().AddToRoute(ws)
	file.NewFileHandler().AddToRoute(ws)
	return ws
}

func (r *Router) initLogin() *restful.WebService {
	ws := new(restful.WebService)
	ws.Path("/api")
	user.AddLoginToRoute(ws)
	return ws
}

func (r *Router) initWsconn() *restful.WebService {
	ws := new(restful.WebService)
	ws.Path("/ws")
	cluster.AddInstallToRoute(ws)
	host.AddInstallToRoute(ws)
	return ws
}
